package ntd.block.server.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.SimpleServer;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import io.netty.handler.codec.http.FullHttpRequest;
import lj.netty.ComNettyHttp;
import lj.netty.bean.LJNettyHttpContext;
import lj.netty.tool.LJTNettyHttpParse;
import ntd.block.server.ComServer;
import ntd.block.server.bo.ComServerHttpMsgBo;
import ntd.block.server.bo.ComServerInBo;
import ntd.block.server.bo.ComServerMsgBo;
import ntd.block.server.context.NtdContextAction;
import ntd.block.server.enums.EnumHttpDataType;
import ntd.util.UtilThreadPool;

import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 交互应用 - http 实现
 */
public class ComServerHttp extends ComServerBase implements ComServer {

    /** http-server 服务对象 */
    private ComNettyHttp comNettyHttp;

    @Override
    public void init(ComServerInBo comServerInBo) {
        if (ObjectUtil.isNull(comServerInBo)) {
            return;
        }
        this.comServerInBo = comServerInBo;
        startServer();
    }

    /**
     * 启动服务
     */
    private void startServer() {
        comNettyHttp = new ComNettyHttp(comServerInBo.getPort()) {
            @Override
            public String onMsg(LJNettyHttpContext ljNettyHttpContext) {
                FullHttpRequest request = ljNettyHttpContext.getRequest();
                StaticLog.info(ljNettyHttpContext.getRequest().uri());

                // 调用监听函数
                ComServerMsgBo comServerMsgBo = new ComServerMsgBo();
                comServerMsgBo.setKey(request.uri());
                comServerMsgBo.setMsg(JSONUtil.toJsonStr(buildData(ljNettyHttpContext)));

                StaticLog.info("http请求数据[{}]", JSONUtil.toJsonStr(comServerMsgBo));

                AtomicReference<String> atomicReference = new AtomicReference<>("ok");
                comServerListenList.forEach(comServerListen -> {
                    ComServerMsgBo comServerMsgBoRes = comServerListen.onMsg(comServerMsgBo);
                    if (ObjectUtil.isNotNull(comServerMsgBoRes)) {
                        atomicReference.set(comServerMsgBoRes.getMsg());
                    }
                });

                return atomicReference.get();
            }
        };

        UtilThreadPool.run(comNettyHttp::start);

        StaticLog.info("http服务[{}]启动成功！", comServerInBo.getPort());
    }

    @Override
    public void close() {
        comNettyHttp.close();
    }

    public void addAction(NtdContextAction ntdContextAction) {
        if (ObjectUtil.isNull(ntdContextAction)) {
            return;
        }
        StaticLog.info("action[{}]附加成功！", ntdContextAction.getPath());
    }

    private static String getData(HttpServerRequest request, EnumHttpDataType httpDataType) {
        switch (httpDataType) {
            case JSON:
                return request.getBody();
            case FORM:
                return JSONUtil.toJsonStr(request.getMultipart().getParamListMap());
            case URL:
                return JSONUtil.toJsonStr(request.getParams());
        }
        return "";
    }

    private static ComServerHttpMsgBo buildData(LJNettyHttpContext request) {
        ComServerHttpMsgBo comServerHttpMsgBo = new ComServerHttpMsgBo();
        comServerHttpMsgBo.setForm(JSONUtil.toJsonStr(LJTNettyHttpParse.readPostParams(request.getRequest())));
        comServerHttpMsgBo.setParam(JSONUtil.toJsonStr(LJTNettyHttpParse.readGetParams(request.getRequest())));
        return comServerHttpMsgBo;
    }

    public static void main(String[] args) {
        ComServerInBo comServerInBo1 = new ComServerInBo();
        comServerInBo1.setPort(36019);
        ComServerHttp comServerHttp = new ComServerHttp();
        comServerHttp.init(comServerInBo1);
    }
}
